﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DriveBeamEditor.Controls.General.Data
{
    public class TreeNodeCollection : KeyedCollection<string, TreeNode>
    {
        #region Fields

        public event EventHandler<TreeNode> TreeNodeAdded;
        public event EventHandler<TreeNode> TreeNodeRemoved;

        #endregion

        #region Properties

        public TreeNode Parent { get; private set; }

        #endregion

        #region CTORs

        public TreeNodeCollection(TreeNode parent)
        {
            Parent = parent;
        }

        #endregion

        #region Methods

        public new void Add(TreeNode node)
        {
            node.Parent = Parent;
            base.Add(node);
            if (TreeNodeAdded != null)
                TreeNodeAdded(this, node);
        }

        public new void Remove(TreeNode node)
        {
            node.Parent = null;
            base.Remove(node);
            if (TreeNodeRemoved != null)
                TreeNodeRemoved(this, node);
        }

        public new void RemoveAt(int index)
        {
            var node = this[index];
            if (node != null) node.Parent = null;
            base.RemoveAt(index);
            if (TreeNodeRemoved != null)
                TreeNodeRemoved(this, node);
        }

        public new void Clear()
        {
            foreach (var node in this)
                node.Parent = null;
            base.Clear();
        }

        public new void ClearItems()
        {
            foreach (var node in this)
                node.Parent = null;
            base.ClearItems();
        }

        #endregion

        #region KeyedCollection

        protected override string GetKeyForItem(TreeNode item)
        {
            return item.Path;
        }

        #endregion
    }
}
